package com.demo.apparel.service.uniapp.Impl;

import com.demo.apparel.entity.TempImage;
import com.demo.apparel.mapper.uniapp.TempImageMapper;
import com.demo.apparel.service.uniapp.TempImageService;
import com.demo.apparel.utils.COSUploadUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class TempImageServiceImpl implements TempImageService {

    private final TempImageMapper tempImageMapper;

    public TempImageServiceImpl(TempImageMapper tempImageMapper) {
        this.tempImageMapper = tempImageMapper;
    }

    @Override
    public String uploadTempImage(MultipartFile file, String userId) throws IOException {
        // 1. 上传图片到腾讯云 COS，得到访问 URL
        String url = COSUploadUtil.upload(file, "temp");

        // 2. 生成唯一ID
        String id = UUID.randomUUID().toString().replace("-", "");

        // 3. 构建实体对象
        TempImage tempImage = new TempImage();
        tempImage.setId(id);
        tempImage.setUrl(url);
        tempImage.setUserId(userId); // 一定要设置userId
        tempImage.setCreatedTime(LocalDateTime.now());

        // 4. 你可以用 userId 关联记录，比如加个字段，或者做别的业务处理
        // 这里示例没加 userId 字段，如果表里有，可以设置

        // 5. 插入数据库
        tempImageMapper.insert(tempImage);

        // 6. 返回图片访问 URL
        return url;
    }

}
